package com.hhhn.wk.h5;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.hhhn.wk.R;
import com.hhhn.wk.base.BaseActivity;
/**
 * * * * * * * * * * * *
 * @author：文珂    ｡◕‿◕｡ *
 * Data：2018/1/31 0031    ｡◕‿◕｡ *
 * Function：      ｡◕‿◕｡ *
 * 链接webView
 * * * * * * * * * * * */
public class LinkH5Activity extends BaseActivity {
    private ProgressBar pg1;
    private TextView tv_title;//标题
    private String title="";
    private String url="";
//    private String url="http://192.168.0.251/hn-web/hn-dynamic.html?id=0050e330-97c5-47d8-94bf-3cd063abbb4b";
    private WebView webView;
    @Override
    protected void setOnCreateContentView() {
        setContentView(R.layout.activity_link_h5);
        Intent intent=getIntent();
        title = intent.getStringExtra("title");
        url = intent.getStringExtra("url");
        Log.d("", "setOnCreateContentView: "+url);
    }
    @Override
    protected void initView() {
        tv_title =findViewById(R.id.tv_title);
        webView =findViewById(R.id.webView);
        pg1=findViewById(R.id.progressBar1);
    }
    @Override
    protected void initData() {
        tv_title.setText(title);
        WebSettings settings = webView.getSettings();
        //设置WebView属性,运行执行js脚本
        settings.setJavaScriptEnabled(true);
        //支持屏幕缩放
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        //不显示webview缩放按钮
        settings.setDisplayZoomControls(false);
        //调用loadUrl方法为WebView加入链接
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {
            //设置在webView点击打开的新网页在当前界面显示,而不跳转到新的浏览器中
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress==100){
                    //加载完网页进度条消失
                    pg1.setVisibility(View.GONE);
                }
                else{
                    //开始加载网页时显示进度条
                    pg1.setVisibility(View.VISIBLE);
                    //设置进度值
                    pg1.setProgress(newProgress);
                }
            }
        });
    }
    @Override
    protected void initClick() {
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            if (webView != null) {
                webView.removeAllViews();
                webView.destroy();
                webView = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
